import java.util.Stack;

/*
 * @lc app=leetcode.cn id=150 lang=java
 *
 * [150] 逆波兰表达式求值
 */

// @lc code=start
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for ( String token: tokens){
            if ( isDigit(token) ){
                stack.push(Integer.valueOf(token));
            }else {
                if ( token.equals("+")){
                    Integer item1 = stack.pop();
                    Integer item2 = stack.pop();
                    stack.push(item1+item2);
                }else if ( token.equals("-") ){
                    Integer item1 = stack.pop();
                    Integer item2 = stack.pop();
                    stack.push(item2-item1);
                }else if ( token.equals("*") ){
                    Integer item1 = stack.pop();
                    Integer item2 = stack.pop();
                    stack.push(item2*item1);
                }else if ( token.equals("/")){
                    Integer item1 = stack.pop();
                    Integer item2 = stack.pop();
                    stack.push(item2/item1);
                }
            }
        }
        Integer integer =  stack.pop();
        return integer;
    }
    public boolean isDigit(String str){
        char[] chs = str.toCharArray();
        for ( char ch: chs){
            if ( !(Character.isDigit(ch)) &&  chs.length == 1 ){
                return false;
            }
        }
        return true;
    }
}
// @lc code=end

